package com.ruoyi.jl_base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jl_base.domain.bo.JlBaseBsContractBo;
import com.ruoyi.jl_base.domain.vo.JlBaseBsContractVo;
import com.ruoyi.jl_base.domain.JlBaseBsContract;
import com.ruoyi.jl_base.mapper.JlBaseBsContractMapper;
import com.ruoyi.jl_base.service.IJlBaseBsContractService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 合同主Service业务层处理
 *
 * @author zyb
 * @date 2024-12-23
 */
@RequiredArgsConstructor
@Service
public class JlBaseBsContractServiceImpl implements IJlBaseBsContractService {

    private final JlBaseBsContractMapper baseMapper;

    /**
     * 查询合同主
     */
    @Override
    public JlBaseBsContractVo queryById(Long contractId){
        return baseMapper.selectVoById(contractId);
    }

    /**
     * 查询合同主列表
     */
    @Override
    public TableDataInfo<JlBaseBsContractVo> queryPageList(JlBaseBsContractBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JlBaseBsContract> lqw = buildQueryWrapper(bo);
        Page<JlBaseBsContractVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询合同主列表
     */
    @Override
    public List<JlBaseBsContractVo> queryList(JlBaseBsContractBo bo) {
        LambdaQueryWrapper<JlBaseBsContract> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JlBaseBsContract> buildQueryWrapper(JlBaseBsContractBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JlBaseBsContract> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, JlBaseBsContract::getDeptId, bo.getDeptId());
        lqw.eq(bo.getProjectId() != null, JlBaseBsContract::getProjectId, bo.getProjectId());
        lqw.eq(bo.getBdId() != null, JlBaseBsContract::getBdId, bo.getBdId());
        lqw.eq(StringUtils.isNotBlank(bo.getContractNo()), JlBaseBsContract::getContractNo, bo.getContractNo());
        return lqw;
    }

    /**
     * 新增合同主
     */
    @Override
    public Boolean insertByBo(JlBaseBsContractBo bo) {
        JlBaseBsContract add = BeanUtil.toBean(bo, JlBaseBsContract.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setContractId(add.getContractId());
        }
        return flag;
    }

    /**
     * 修改合同主
     */
    @Override
    public Boolean updateByBo(JlBaseBsContractBo bo) {
        JlBaseBsContract update = BeanUtil.toBean(bo, JlBaseBsContract.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JlBaseBsContract entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除合同主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
